﻿using Lemon.Blog.Account.Dtos;
using Lemon.Blog.Social.Dtos;
using Lemon.Blog.Users;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;

namespace Lemon.Blog.Social
{
    [Authorize]
    public class SocialService: BlogAppService, ISocialService
    {
        private readonly IFreeSql _freeSql;

        public SocialService(IFreeSql freeSql)
        {
            this._freeSql = freeSql;
        }

        /// <summary>
        /// 关注
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task Follow(Guid userId)
        {
            if(CurrentUser.Id == userId)
                throw new UserFriendlyException("不要自恋到关注自己！");
            SocialRelationship socialRelationship = new()
            {
                UserIdFans = (Guid)CurrentUser.Id,
                UserIdFollowed = userId
            };
            await _freeSql.Insert(socialRelationship).ExecuteAffrowsAsync();
        }

        /// <summary>
        /// 取消关注
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UnFollow(Guid userId)
        {
            await _freeSql.Delete<SocialRelationship>()
                .Where(t => t.UserIdFans == (Guid)CurrentUser.Id && t.UserIdFollowed == userId)
                .ExecuteAffrowsAsync();
        }

        /// <summary>
        /// 是否关注
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<bool> IsFollow(Guid userId)
        {
            return await _freeSql.Select<SocialRelationship>()
                .Where(t => t.UserIdFans == (Guid)CurrentUser.Id && t.UserIdFollowed == userId)
                .AnyAsync();
        }

        /// <summary>
        /// 移除粉丝
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task RemoveFans(Guid userId)
        {
            await _freeSql.Delete<SocialRelationship>()
                .Where(t => t.UserIdFollowed == (Guid)CurrentUser.Id && t.UserIdFans == userId)
                .ExecuteAffrowsAsync();
        }

        /// <summary>
        /// 关注列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<UserInfoDto>> GetFollowList(FollowAndFanListDto dto)
        {
            var userList = await _freeSql.Select<SocialRelationship, AppUser>()
                .LeftJoin(t => t.t1.UserIdFollowed == t.t2.Id)
                .Where(t => t.t1.UserIdFans == CurrentUser.Id)
                .Count(out var total)
                .Page((dto.SkipCount / dto.MaxResultCount) + 1, dto.MaxResultCount)
                .ToListAsync(t => t.t2);
            List<UserInfoDto> result = ObjectMapper.Map<List<AppUser>, List<UserInfoDto>>(userList);
            return new PagedResultDto<UserInfoDto>() { Items = result, TotalCount = total };
        }

        /// <summary>
        /// 粉丝列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<UserInfoDto>> GetFansList(FollowAndFanListDto dto)
        {
            var userList = await _freeSql.Select<SocialRelationship, AppUser>()
                .LeftJoin(t => t.t1.UserIdFollowed == t.t2.Id)
                .Where(t => t.t1.UserIdFollowed == CurrentUser.Id)
                .Count(out var total)
                .Page((dto.SkipCount / dto.MaxResultCount) + 1, dto.MaxResultCount)
                .ToListAsync(t => t.t2);
            List<UserInfoDto> result = ObjectMapper.Map<List<AppUser>, List<UserInfoDto>>(userList);
            return new PagedResultDto<UserInfoDto>() { Items = result, TotalCount = total };
        }
    }
}
